﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using bba.framework.infraweb.entities;
using bba.framework.infraweb.interfaces.controller;
using Microsoft.Practices.Unity;

namespace bba.framework.infraweb.masterpage.Pages
{
    public partial class Alerts : System.Web.UI.Page
    {
        #region Fields
        protected List<Alert> ListViewState
        {
            set { ViewState["lista"] = value; }
            get
            {
                object o = ViewState["lista"];
                if (o == null)
                    return null;
                else
                    return (List<Alert>)o;
            }
        }
        #endregion

        #region Properties

        /// <summary>
        /// Propriedade responsável por definir os métodos e 
        /// eventos da página <seealso cref="bba.framework.infraweb.masterpage.Pages.Alerts"/>
        /// </summary>
        [Dependency]
        public IAlertPageController PageController { get; set; }

        #endregion

        #region Methods Private

        private void ClearControls()
        {
            txtDescricao.Text = string.Empty;
            txtLink.Text = string.Empty;
            scheduleTxt.Value = string.Empty;
            txtSubTitulo.Text = string.Empty;
            txtTitulo.Text = string.Empty;
            txtUserName.Text = string.Empty;
            horarioTxt.Value = string.Empty;
            foreach (ListItem item in ckbSystemList.Items)
                item.Selected = false;
        }

        #endregion

        #region Events

        protected void ckbGroupTodos_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbGroupTodos.Checked)
            {
                foreach (ListItem item in ckbSystemList.Items)
                    item.Selected = true;
            }
            else
            {
                foreach (ListItem item in ckbSystemList.Items)
                    item.Selected = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = string.Empty;
            lblMessage.Visible = false;
            if (!IsPostBack)
            {
                txtUserName.Enabled = false;
                ckbUsers.Checked = false;
                ckbGroup.Checked = true;
                ckbGroupTodos.Checked = false;
                PageController.LoadSystems(this.ckbSystemList);
            }
            ListViewState = PageController.LoadGridView(this.alertDataGridView);
        }

        protected void ckbUsers_CheckedChanged(object sender, EventArgs e)
        {
            PageController.SelectUsers(ckbGroupTodos, ckbGroup, ckbSystemList, txtUserName);
        }

        protected void ckbGroup_CheckedChanged(object sender, EventArgs e)
        {
            PageController.SelectGroup(txtUserName, ckbUsers, ckbGroupTodos, ckbSystemList);
        }

        protected void adicionarImgButton_Click(object sender, ImageClickEventArgs e)
        {
            if (PageController.VerifyFields(this.ckbUsers, this.ckbGroup, this.ckbSystemList, this.txtTitulo, this.txtDescricao, this.txtUserName,
                        this.scheduleTxt, this.horarioTxt, this.lblMessage))
                if (Page.IsValid)
                {
                    PageController.InsertAlert(this.hiddenIdAlert, this.ckbSystemList, this.txtTitulo, this.txtLink, this.txtSubTitulo,
                            this.txtDescricao, txtUserName, this.scheduleTxt, this.horarioTxt, this.updateButton,
                            this.newButton, this.adicionarImgButton, this.lblMessage);
                    ListViewState = PageController.LoadGridView(this.alertDataGridView);
                    ClearControls();
                }
        }

        protected void alertDataGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            alertDataGridView.PageIndex = e.NewPageIndex;
            alertDataGridView.DataSource = ListViewState;
            alertDataGridView.DataBind();
        }

        protected void alertDataGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            PageController.DeleteAlert(e.RowIndex, this.alertDataGridView,
                this.lblMessage, this.updateButton, this.newButton, this.adicionarImgButton);
            ListViewState = PageController.LoadGridView(this.alertDataGridView);
            ClearControls();
        }

        protected void cancelarImgButton_Click(object sender, ImageClickEventArgs e)
        {
            ClearControls();
        }

        protected void alertDataGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            updateButton.Visible = true;
            newButton.Visible = true;
            adicionarImgButton.Visible = false;
            ClearControls();
            PageController.LoadAlertEdit(e.NewEditIndex, this.alertDataGridView, this.hiddenIdAlert, this.txtTitulo, this.txtLink, this.txtSubTitulo,
                this.txtDescricao, this.txtUserName, this.scheduleTxt, this.horarioTxt, this.updateButton, this.newButton, this.adicionarImgButton,
                this.lblMessage, this.ckbGroup, this.ckbUsers, ckbSystemList);
        }

        protected void ckbSystemList_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtUserName.Text = string.Empty;
        }

        protected void updateButton_Click(object sender, ImageClickEventArgs e)
        {
            if (PageController.VerifyFields(this.ckbUsers, this.ckbGroup, this.ckbSystemList, this.txtTitulo, this.txtDescricao, this.txtUserName,
                        this.scheduleTxt, this.horarioTxt, this.lblMessage))
                if (Page.IsValid)
                {
                    PageController.UpdateAlert(this.hiddenIdAlert, this.ckbSystemList, this.txtTitulo, this.txtLink, this.txtSubTitulo,
                                                this.txtDescricao, txtUserName, this.scheduleTxt, this.horarioTxt, this.updateButton,
                                                this.newButton, this.adicionarImgButton, this.lblMessage);
                    ListViewState = PageController.LoadGridView(this.alertDataGridView);
                    ClearControls();
                }
        }

        protected void newButton_Click(object sender, ImageClickEventArgs e)
        {
            adicionarImgButton.Visible = true;
            updateButton.Visible = false;
            ClearControls();
        }

        #endregion

    }
}
